package ${table.package}.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.math.NumberUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.DynaActionForm;

import cn.com.asb.mdms.base.util.query.QueryInfo;
import cn.com.asb.mdms.base.web.action.BaseFreeDispatchAction;
import ${table.package}.bo.${table.destinationClassName};
import ${table.package}.service.${table.destinationClassName}Manager;


/**
 * 
 * @author mingxian.xu
 *
 */
public class ${table.destinationClassName}Action extends BaseFreeDispatchAction {
	private static Logger logger = Logger.getLogger(${table.destinationClassName}Action.class);

	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return initList(mapping, form, request, response);
	}

	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		DynaActionForm spForm = (DynaActionForm) form;
		${table.destinationClassName} ${table.variableName} = (${table.destinationClassName}) spForm.get("${table.variableName}");
		logger.debug("-----save------- ${table.variableName} : " + ${table.variableName});

		${table.destinationClassName}Manager ${table.variableName}Manager = (${table.destinationClassName}Manager) getBean("${table.destinationClassName}Manager");
		if(${table.variableName}.getId()!=null && ${table.variableName}.getId().longValue()!=0){
			${table.variableName}Manager.update(${table.variableName});
		}else{
			${table.variableName}Manager.create(${table.variableName});
		}
		
		return mapping.findForward("success");
	}

	public ActionForward initList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.debug("::initList::");
		DynaActionForm spForm = (DynaActionForm) form;
		${table.destinationClassName} ${table.variableName} = new ${table.destinationClassName}();
		spForm.set("${table.variableName}", ${table.variableName});		
		return mapping.findForward("list");
	}

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		DynaActionForm spForm = (DynaActionForm) form;
		${table.destinationClassName} ${table.variableName} = (${table.destinationClassName}) spForm.get("${table.variableName}");
		logger.debug("${table.variableName} : " + ${table.variableName});
		QueryInfo info = setUpQueryParameters4List(${table.variableName});
		
		int pageSize = NumberUtils.stringToInt(
				request.getParameter("pageSize"), 20);
		int currentPage = NumberUtils.stringToInt(getDisplayTagParam(request,
				"d-(\\d)+-p"), 1);
		info.setLimit(new Integer(pageSize));
		info.setOffset(new Integer(pageSize * (currentPage - 1)));

		${table.destinationClassName}Manager ${table.variableName}Manager = (${table.destinationClassName}Manager) getBean("${table.destinationClassName}Manager");
		List result = ${table.variableName}Manager.query${table.destinationClassName}(info);
		logger.debug("reslut size : " + result.size());
		request.setAttribute("searchResult", result);
		
		return mapping.findForward("list");
	}

	
	public ActionForward view(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		${table.destinationClassName}Manager ${table.variableName}Manager = (${table.destinationClassName}Manager) getBean("${table.destinationClassName}Manager");
		DynaActionForm spForm = (DynaActionForm) form;
		${table.destinationClassName} ${table.variableName} = (${table.destinationClassName}) spForm.get("${table.variableName}");
		spForm.set("${table.variableName}", ${table.variableName}Manager.get(new Long(${table.variableName}.getId())));
		logger.debug("view ${table.variableName} :" + ${table.variableName});		
		return mapping.findForward("list");
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.info("--------deleteAll----------");
		String[] id = request.getParameterValues("selectIdx");
		if (id != null){
			for (int i = 0; i < id.length; i++) {
				logger.info(":" + id[i]);
			}
		}

		try {
			${table.destinationClassName}Manager ${table.variableName}Manager = (${table.destinationClassName}Manager) getBean("${table.destinationClassName}Manager");
			for (int i = 0; i < id.length; i++) {
				${table.variableName}Manager.delete(new Long(Long.parseLong(id[i])));
			}
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
//			ActionMessages messages = new ActionMessages();
//			messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
//					"errors.detail", "error parameter"));
//			saveMessages(request, messages);
		}
		return list(mapping, form, request, response);
	}


	private QueryInfo setUpQueryParameters4List(${table.destinationClassName} ${table.variableName}) {
		QueryInfo info = new QueryInfo();
//		fillMapWithoutEmptyValue(info, "name", ${table.variableName}.getName());
//		fillMapWithoutEmptyValue(info, "canControl", ${table.variableName}.getCanControl(),"-2");
//		fillMapWithoutEmptyValue(info, "canSnatchMulti", ${table.variableName}.getCanSnatchMulti(),"-2");
//		fillMapWithoutEmptyValue(info, "canSnatchSingle", ${table.variableName}.getCanSnatchSingle(),"-2");
//		fillMapWithoutEmptyValue(info, "canView", ${table.variableName}.getCanView(),"-2");
//		fillMapWithoutEmptyValue(info, "canWarn", ${table.variableName}.getCanWarn(),"-2");
		logger.debug(">>>>>>>>> info <<<<<<<<<<<< : " + info);
		return info;
	}
}